package cn.com.algery.temp.web;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.algery.temp.domain.Page;
import cn.com.algery.temp.domain.Server;
import cn.com.algery.temp.service.ServerService;

/**
 * Controller
 */
@Controller
@RequestMapping(value = "/server")
public class ServerController {
	@Resource
	private ServerService serverService;

    @RequestMapping("/view")
    public String view() {
        return "serverDetail";
    }
    
    @RequestMapping(value = "/addServer",method = RequestMethod.POST)
    public @ResponseBody Boolean addServer(@RequestBody Server server,HttpSession session) {
    	Subject subject = SecurityUtils.getSubject();
    	try {
    		System.out.println("add_server_name:"+server.getServer_name());
			return serverService.addServer(server,subject.getPrincipal().toString());
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
    }
    
    @RequestMapping(value = "/checkServerName",method = RequestMethod.GET)
    public @ResponseBody Boolean isValidServerName(HttpServletRequest request,HttpSession session) {
    	String serverName = request.getParameter("serverName");
    	if(serverName == null || "".equals(serverName) ) {
    		return false;
    	}
    	return serverService.isUnExistsedServer(serverName);
    }
    
    @RequestMapping(value="/deleteServer",method = RequestMethod.DELETE)
    public @ResponseBody Boolean deleteServer(HttpServletRequest request) {
    	return serverService.deleteServerByName(request.getParameter("serverName"));
		
	}
    
    @RequestMapping(value="/findAllServer", method = RequestMethod.GET)
    public @ResponseBody List<Server> findAllServer(HttpServletRequest request) {
    	List<Server> servers = serverService.findAll();
    	return servers;
    }
    
    @RequestMapping(value="/findServerByName",method = RequestMethod.GET)
    public @ResponseBody Server findServerByName(HttpServletRequest request) {
    	String serverName = (String) request.getParameter("serverName");
    	Server server = serverService.findServerByName(serverName);//serverService.findServerByName(serverName);
		return server;
    }
    
    @RequestMapping(value="/queryServerPag",method = RequestMethod.GET)
    public @ResponseBody Map<String, Object> findServerByName(HttpServletRequest request,Page page) {
    	Map<String, Object> map = serverService.queryServerInList(page);//serverService.findServerByName(serverName);
		return map;
    }
    
    @RequestMapping(value="/queryServerByName",method = RequestMethod.GET)
    public @ResponseBody List<Server> queryServerByName(HttpServletRequest request) {
    	String serverName = (String) request.getParameter("serverName");
    	List<Server> servers = serverService.queryServerByName(serverName);
		return servers;
    	
    }
    
	@RequestMapping(value = "/updateServer", method = RequestMethod.PUT)
    public @ResponseBody Server update(@RequestBody Server server, HttpSession session) {
		if(serverService.updateServer(server)) {
			return server;
		} else {
			return new Server();
		}
    }
	
	@RequestMapping(value = "/queryMyTodoList", method = RequestMethod.GET)
    public @ResponseBody Map<String, Object> queryMyTodoList(HttpSession session,Page page) {
    	Subject subject = SecurityUtils.getSubject();
		return serverService.queryMyTodoList(subject.getPrincipal().toString(),page);
    }
	
	@RequestMapping(value = "/queryMyApplication", method = RequestMethod.GET)
    public @ResponseBody Map<String, Object> queryMyApplication(HttpSession session,Page page) {
    	Subject subject = SecurityUtils.getSubject();
		return serverService.queryMyApplication(subject.getPrincipal().toString(),page);
    }
	
	@RequestMapping(value = "/approveServer", method = RequestMethod.PUT)
    public @ResponseBody Boolean approveServer(HttpSession session,@RequestBody Map<String, Object>	map) {
		return serverService.approve(map.get("taskId").toString(),map.get("isApproval").toString(),null);
    }

}
